package com.symria.system.email;

import java.io.File;
import java.io.IOException;
import java.io.StringWriter;
import java.io.Writer;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Locale;
import java.util.Map;

import javax.servlet.ServletContext;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.util.ServletContextAware;

import com.ctc.wstx.util.StringUtil;
import com.symria.framework.util.SpringUtil;

import freemarker.template.Configuration;
import freemarker.template.Template;

public class FreemarkerUtil {
	private Configuration cfg = null;
	private static ServletContext context;
	private static Map <String, FreemarkerUtil> utils = new HashMap<String, FreemarkerUtil>();
	private final static Log log = LogFactory.getLog(FreemarkerUtil.class);
	private final static String encoding = "utf-8";
    
	/**
	 * 获取freemarker的配置. freemarker本身支持classpath,目录和从ServletContext获取.
	 */
	private FreemarkerUtil(Class clasz, String path) {
		cfg = new Configuration();
		cfg.setClassForTemplateLoading(clasz, path);
		cfg.setEncoding(Locale.getDefault(), encoding);
	}
	
	private FreemarkerUtil(ServletContext context, String path) {
		cfg = new Configuration();
		cfg.setServletContextForTemplateLoading(context, path);
		cfg.setEncoding(Locale.getDefault(), encoding);
	}
	
	private FreemarkerUtil(String path) {
		try {
			File file = new File(path);
			if(!file.exists()){
				file.mkdirs();
			}
			cfg = new Configuration();			
			cfg.setDirectoryForTemplateLoading(file);
		} catch (IOException e) {
			log.error("设置模板出错", e);
		}
		cfg.setEncoding(Locale.getDefault(), encoding);
	}
	
	public static FreemarkerUtil getInstance(String path){
		String key = getContext().getRealPath(path);
		FreemarkerUtil util = utils.get(key);
		if(util==null){
			util = new FreemarkerUtil(key);
			utils.put(key, util);		
		}
		return util;
	}	

	public static FreemarkerUtil getInstance(ServletContext context, String path){
		String key = path;
		FreemarkerUtil util = utils.get(key);
		if(util==null){
			util = new FreemarkerUtil(context, path);
			utils.put(key, util);			
		}
		return util;
	}
	
	/*释放内存*/
	public static void shutdown(){
		Iterator <String> it= utils.keySet().iterator();
		while (it.hasNext()) {
			String key = (String) it.next();
			FreemarkerUtil util = utils.get(key);
			if(util!=null){
				util.destroy();
			}
		}
		utils.clear();
		utils = null;
	}
	
	public void destroy(){
		cfg = null;
	}
	
	public String process(String templateFileName, Map propMap){
		try {
			Template t  = cfg.getTemplate(templateFileName);
			Writer out = new StringWriter();
			t.process(propMap, out);
			out.flush();
			out.close();
			return out.toString();
		} catch (Exception e) {
			log.error("生成模板出错!"+templateFileName, e);
		}
		return null;
	}
	
	public static void main(String[] args) throws IOException {
		Map root = null;
		String email = FreemarkerUtil.getInstance("D:/Tomcat 6.0/market/ROOT/template").process("email/success.html", root);
		System.out.println(email);
	}

	public static ServletContext getContext(){
		return SpringUtil.getServletContext();
	}

}
